{layout name="public/layout" /}
{include file='yxnavbar'}


<div class="main">
    {include file='sms/sms_header'}
    <div class="panel panel-info">
        <div class="panel-heading">筛选 共{$total}条
        </div>
        <div class="panel-body">
            <form action="./index.php" method="get" class="form-horizontal" role="form">
                <input type="hidden" name="c" value="site" />
                <input type="hidden" name="a" value="entry" />
                <input type="hidden" name="m" value="{$_P['m']}" />
                <input type="hidden" name="do" value="{$do}" />
                <input type="hidden" name="ac" value="search" />
                <input type="hidden" name="themeid" value="{$themeid}" />

                <div class="form-group">
                    <label class="col-xs-1 col-sm-1 col-md-1 control-label">模版状态</label>
                    <div class="col-xs-11 col-sm-9 col-md-9">
                        <div class="btn-group">
                            {loop $status_list $row}
                            <a href="{php echo createWebUrl('sms',array('op' => 'template_list','themeid'=>$themeid,'template_status'=>$row['key']));}" class="btn {if $template_status == $row['key']}btn-primary{else}btn-default{/if}">
                                {$row['title']}
                                <span style="color: #ff0000">{$row['total']}</span>
                            </a>
                            {/loop}
                        </div>
                    </div>
                </div>
                <div class="form-group">
                    <label class="col-xs-12 col-sm-3 col-md-1 control-label">关键字</label>
                    <div class="col-sm-9 col-xs-2 col-md-3">
                        <input class="form-control" name="search_data" placeholder="短信模版使用说明" type="text" value="{$_P['search_data']}">
                    </div>
                    <div class="col-sm-3 col-lg-3 col-md-3 col-xs-6">
                        <button class="btn btn-default"><i class="fa fa-search"></i> 搜索</button>
                        <input type="hidden" name="__token__" value="{$Request.token}"/>
                    </div>
                    <div class="col-sm-3 col-lg-3 col-md-3 col-xs-6">
                        <a href="{php echo createWebUrl('sms',array('op'=>'template_add','themeid'=>$themeid));}" class="btn btn-success">创建高级模版</a>
                    </div>
                </div>
                <span class="help-block" >短信模版创建后，提交给短信运营商审核，具体审核时间由运营商确定</span>
            </form>
        </div>
    </div>
    <div class="tab-content">
        <div class="panel panel-default">
            <div class="panel-body table-responsive">
                <table class="table table-hover">
                    <thead class="navbar-inner">
                    <tr>
                        <th style="width: 80px;">序号</th>
                        <th style="width: 120px;">短信模版类型</th>
<!--                        <th style="width: 100px;">短信模版类型</th>-->
<!--                        <th style="width: 80px;">短信模版名称</th>-->
                        <th style="width: 260px;">短信模版使用说明</th>
                        <th style="width: 320px;">短信模版内容</th>

                        <th style="width: 80px;">状态</th>
                        <th style="width: 80px;">操作人</th>
                        <th style="width: 80px;">操作ip</th>
                        <th style="width: 140px;">创建时间</th>
                        <th style="width: 100px;">操作</th>
                    </tr>
                    </thead>
                    <tbody>
                    {loop $list $item}
                    <tr>
                        <td>{$item['id']}</td>
                        <td>
                            {if $item['show_template_params']==''}
                            <span class="label label-success">快捷模版</span>
                            {else}
                            <span class="label label-warning">高级模版</span>
                            {/if}
                        </td>
<!--                        <td>-->
<!--                            {if $item['template_type']==0}-->
<!--                            <span class="label label-success">验证码</span>-->
<!--                            {elseif $item['template_type']==1}-->
<!--                            <span class="label label-warning">短信通知</span>-->
<!--                            {elseif $item['template_type']==2}-->
<!--                            <span class="label label-warning">推广短信</span>-->
<!--                            {/if}-->
<!--                        </td>-->
<!--                        <td>-->
<!--                            {$item['out_template_name']}-->
<!--                        </td>-->
                        <td>{$item['remark']}</td>
                        <td>{$item['out_template_content']}</td>

                        <td>
                            {if $item['template_status']==0}
                            <span class="label label-danger">审核中</span>
                            {elseif $item['template_status']==1}
                            <span class="label label-success">通过</span>
                            {elseif $item['template_status']==2}
                            <span class="label label-warning">未通过</span>
                            {elseif $item['template_status']==10}
                            <span class="label label-default">取消审核</span>
                            {/if}
                        </td>
                        <td>{$item['admin_name']}</td>
                        <td>{$item['ip']}</td>
                        <td>{$item['created_at']}</td>
                        {if $item['template_status']==0}
                        <td  >
                            {if $item['type']=='quick'}
                            <a class="btn btn-warning btn-sm"
                               href="{php echo createWebUrl('sms',array('op'=>'quick_sms','themeid'=>$themeid,'template_id'=>$item['id']));}"  title="修改"><i class="icon-edit"></i>修改</a>
                            {/if}
                        </td>
                        {/if}
                        {if $item['template_status']==2}
                        <td  >
                            <a class="btn btn-warning btn-sm"
                               href="{php echo createWebUrl('sms',array('op'=>'template_add','themeid'=>$themeid,'template_id'=>$item['id']));}"  title="编辑"><i class="icon-edit"></i>编辑</a>
                        </td>
                        {/if}

                        {if $item['template_status']==1}
                        <td  >
                            {if !$item['has_wait_send']}
                            <a class="btn btn-primary btn-sm"
                               href="{php echo createWebUrl('sms',array('op'=>'betch_ll','themeid'=>$themeid,'template_id'=>$item['id']));}"  title="批量发送"><i class="icon-edit"></i>批量发送</a>
                            {/if}
                            {if $item['has_wait_send']}
                            <a class="btn btn-danger btn-sm" onclick="showWaitSend(this)" data-id="{$item['id']}" title="队列发送"><i class="icon-edit"></i>立即发送</a>
                            {/if}
                        </td>
                        {/if}
                    </tr>
                    {/loop}
                    </tbody>
                </table>
            </div>
        </div>
    </div>
</div>



<script>
    var send_num=1;
    var timer;
    var success_total =0;
    var fail_total =0;
    //获取当前短信模版待发送短信数
    function showWaitSend(e) {
        var sms_template_id = e.getAttribute("data-id");
        var checkSmsTemplateTotalUrl = "{php echo createWebUrl('sms',array('op'=>'template_wait_total','themeid'=>$themeid));}";
        $.ajax({
            url:checkSmsTemplateTotalUrl,
            dataType:'json',
            method:'post',
            data:{template_id:sms_template_id},
            success:function (e) {
                let status = e.status;
                let result = e.result;
                if (status!=1){
                    layer.msg(result.msg)
                }else{
                    var total = result.count;
                    var total_txt = result.total_txt;
                    var item_type_key = result.item_type_key;
                    send_num = result.send_num;
                    var format_html = '<div id="send_sms_process" style="width: 840px;padding: 60px;">\n' +
                        '    <div class="layui-progress layui-progress-big" lay-showPercent="true" >\n' +
                        '        <div class="layui-progress-bar layui-bg-blue" id="show_sms_send_process_rate" style="width: 0%;">\n' +
                        '            <span class="layui-progress-text" id="show_sms_send_process_num">' + total_txt + '</span>\n' +
                        '        </div>\n' +
                        '    </div>\n' +
                        '        <div  id="queue_process_notice" style="text-align: center;display: none;">累计发送成功：100条，失败：1条；</div>\n' +
                        '    <div class="layui-btn-container" style="margin-top: 40px;float: right;">\n' +
                        '        <button type="button" class="layui-btn layui-btn-primary" id="cancel_send" onclick="hideSendSms()">取消发送</button>\n' +
                        '        <button type="button" class="layui-btn" id="sure_send" data-templateid="'+ sms_template_id +'" data-total="'+ total +'" onclick="user_sure_send_quick_sms(this)">确认发送</button>\n' +
                        '        <button type="button" style="display: none;" class="layui-btn" id="jump_send_log" data-typekey="'+ item_type_key +'"  onclick="jump_send_log(this)">发送完毕，查看发送记录</button>\n' +
                        '    </div>\n' +
                        '</div>';
                    layer.open({
                        type:1,
                        title:'快捷短信发送',
                        area: ['840px', '240px'], // 宽高
                        content: format_html
                    });
                }
            },
            fail:function (err) {
                console.log(err)
            }
        })
    }
    function user_sure_send_quick_sms(e) {
        $('#cancel_send').hide();
        $('#sure_send').html('发送执行中...');
        $('#sure_send').attr('disabled',"true");
        var all_process = 0;
        var template_id = e.getAttribute('data-templateid');
        var queue_total = e.getAttribute('data-total');
        var wait_send_num = Math.ceil(queue_total/100);
        var once_add_process = Math.ceil(100/wait_send_num);
        var action_num = 0;
        timer = setInterval(function () {
            var postUrl = "{php echo createWebUrl('sms',array('op'=>'quick_sms_send','themeid'=>$themeid));}";
            $.ajax({
                url:postUrl,
                dataType:'json',
                method:'post',
                data:{template_id:template_id},
                success:function (e) {
                    let status = e.status;
                    let result = e.result;
                    success_total += result.success_total;
                    fail_total += result.fail_total;
                    if (status!=1){
                        clearInterval(timer);
                        $('#sure_send').html('发送异常');
                        layer.msg(result.msg)
                        setTimeout(function () {
                            window.location.reload();
                        },1500);
                    }else{
                        action_num+=1;
                        all_process += once_add_process;
                        if (all_process>100){
                            all_process = 100;
                        }
                        var format_all_process = all_process+'%';
                        $('#show_sms_send_process_rate').width(format_all_process);
                        if (queue_total<100){
                            var process_txt = queue_total+" / "+queue_total;
                        }else{
                            var curt_process_num = action_num*100;
                            if (curt_process_num>queue_total){
                                curt_process_num = queue_total;
                            }
                            var process_txt = curt_process_num+" / "+queue_total;
                        }
                        $("#show_sms_send_process_num").html(process_txt);
                        if (send_num>0){
                            send_num-=1;
                        }
                        if (send_num<1){
                            clearInterval(timer);
                            var show_sms_queue_process_notice = '累计发送成功:'+success_total+'条；发送失败:'+fail_total+'条';
                            $("#queue_process_notice").html(show_sms_queue_process_notice);
                            $("#queue_process_notice").show();
                            $('#sure_send').hide();
                            $('#jump_send_log').show();
                            // layer.msg('短信发送完毕，即将刷新页面');
                            // setTimeout(function () {
                            //     window.location.reload();
                            // },1500);
                        }
                    }
                },
                fail:function (err) {
                    console.log(err)
                    clearInterval(timer);
                    return;
                }
            })
        },'1500');
    }
    function hideSendSms() {
        layer.closeAll();
    }
    function jump_send_log(e) {
        var type_key = e.getAttribute('data-typekey');
        var jump_url = "{php echo createWebUrl('sms',array('op'=>'display','themeid'=>$themeid));}"+'&type='+type_key;
        window.location.href = jump_url;
    }
</script>

